/*
 * File         : cym_clock.c
 * Version      : 0.1.0
 * Author       : Vincent Cheung
 * Date         : Sept. 23, 2013
 * Description  : All the operation of clock tick.
 *
 * Copyright (C) 2013, CYM Co., Ltd.
 *
 * Change Logs  :
 * Date                 Author          Notes
 * Sept. 23, 2013       Vincent         first version
 * Oct. 30, 2013        Vincent         add high precision timer support
 *                                      and soft timer support
 *
 */

#include "cym_system.h"

/* system tick */
static utick_t cym_tick = 0;

extern void cym_timer_check(void);

#ifdef CYM_USING_HIGH_PREC_TIMER
/* tick for high precision timer */
static utick_t cym_tick_high_prec = 0;

extern void cym_high_prec_timer_check(void);
#endif

/**
 * @addtogroup TimerAndClock
 */

/*@{*/

/**
 * This function is used to get the current tick of system.
 *
 * @return current tick of system
 */
utick_t cym_tick_get(void)
{
	return cym_tick;
}

#ifdef CYM_USING_HIGH_PREC_TIMER
/**
 * This function is used to get current tick of high precision timer.
 *
 * @return current tick of high precision timer
 */
utick_t cym_tick_high_prec_get(void)
{
	return cym_tick_high_prec;
}
#endif

/**
 * This function is used to set current tick to tick.
 *
 * @note this function will influence the hardware timer in system
 *
 * @param tick the tick to set
 *
 * @retval NULL
 */
void cym_tick_set(utick_t tick)
{
	cym_tick = tick;
}

/**
 * This function should be invoked by clock ISR when there is a tick passed.
 *
 * @retval NULL
 */
void cym_tick_increase(void)
{
	++ cym_tick;

	cym_timer_check();
}

#ifdef CYM_USING_HIGH_PREC_TIMER
/**
 * This function is invoked by clock ISR when there's a tick passed
 *
 * @note this function must invoked in high precision timer ISR
 *
 * @retval NULL
 */
void cym_tick_high_prec_increase(void)
{
	++ cym_tick_high_prec;

	cym_high_prec_timer_check();
}
#endif

/*@}*/
